package cn.neusoft.service.impl;

import cn.neusoft.mapper.BusinessMapper;
import cn.neusoft.pojo.Business;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.service.IBusinessService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

public class BusinessService implements IBusinessService {
	@Autowired
	private BusinessMapper businessMapper;
	@Transactional
	public ResultBean add(Business business) throws Exception {
		if(business==null ||
				StringUtils.isEmpty(business.getBusinessName())||
				StringUtils.isEmpty(business.getBusinessAccount())) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=businessMapper.add(business);
		return count>0?ResultBean.SUCCESS(business.getBusinessId()):ResultBean.ERROR("add is failed");
	}
	@Transactional
	public ResultBean update(Business business) throws Exception {
		if(business==null ||
				business.getBusinessId()==null||
				business.getBusinessId()==0 ||
				StringUtils.isEmpty(business.getBusinessName())||
				StringUtils.isEmpty(business.getBusinessAccount())) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=businessMapper.update(business);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("update is failed");
	}

	@Transactional
	public ResultBean delete(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		int count=businessMapper.delete(id);
		return count>0?ResultBean.SUCCESS():ResultBean.ERROR("delete is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getById(Integer id) throws Exception {
		if(id==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		Business business = businessMapper.getById(id);
		return business!=null?ResultBean.SUCCESS(business):ResultBean.ERROR("getById is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getAll() throws Exception {
		List<Business>  list=businessMapper.getAll();
		return list!=null?ResultBean.SUCCESS(list):ResultBean.ERROR("getAll is failed");
	}
	@Transactional(readOnly = true)
	public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
		PageBean<Business> pageBean=new PageBean<Business>();
		if(baseQuery==null) {
			baseQuery=new BusinessQuery();
		}
		int count=businessMapper.getCount(baseQuery);
		pageBean.setTotalRows(count);
		//这里为什么不需要进行计算总页数和总记录数
		List<Business> list= businessMapper.getByPage(baseQuery);
		pageBean.setData(list);
		return ResultBean.SUCCESS(pageBean);
	}

	@Transactional
	public ResultBean batchDel(List<Integer> ids) throws Exception {
		if(ids==null) {
			return ResultBean.ERROR("请求参数为空");
		}
		for ( Integer len: ids) {
			int delete = businessMapper.delete(len);
			if(delete==0) {
				System.out.println("删除商家失败");
			}
		}
		return ResultBean.SUCCESS();
	}
}
